package cn.vote.service;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.List;

import cn.vote.dao.VoteinfoDao;
import cn.vote.domain.Voteinfo;
import cn.vote.domain.Voterresult;

public class VoteinfoService {
	private VoteinfoDao voteinfoDao = new VoteinfoDao();
	/**
	 * 根据类别查询出作品
	 * @param category
	 * @return
	 */
	public List<Voteinfo> getWorksInfo(int category){
		try {
			return voteinfoDao.getWorksInfo(category);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public void insert(String worksid,String voter,int category){
		try {
			voteinfoDao.insert(worksid, voter, category);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public int getPoll(String worksid,int category){
		try {
			return voteinfoDao.getPoll(worksid, category);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public String getPercent(String worksid,int category){
		try {
			int p = voteinfoDao.getPoll(worksid, category);//得到票数
			int poll = p*100;
			int total = voteinfoDao.getTotal();//得到总票数
			DecimalFormat df = new DecimalFormat("0.00");//格式化小数    
	        String percent = df.format((float)poll/total);//返回的是String类型   
	        return percent;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public Voterresult getInfo(String worksid,int category){
		try {
			return voteinfoDao.getInfo(worksid, category);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public Voteinfo getWorks(String worksid,int category){
		try {
			 return voteinfoDao.getWorks(worksid, category);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public Voterresult judge(String username) {
		 try {
			return voteinfoDao.judge(username);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
